﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Kotonoha.Server.Examples {
    public class ExamplesIndex : IEnumerable<KeyValuePair<string, IndexEntry>> {
        private readonly Dictionary<string, IndexEntry> index = new Dictionary<string, IndexEntry>();

        public IndexEntry this[string s] {
            get {
                if (!index.ContainsKey(s)) {
                    IndexEntry entry = new IndexEntry();
                    index.Add(s, entry);
                    return entry;
                }
                return index[s];
            }
        }

        IEnumerator<KeyValuePair<string, IndexEntry>> IEnumerable<KeyValuePair<string, IndexEntry>>.GetEnumerator() {
            return index.GetEnumerator();
        }

        public IEnumerator GetEnumerator() {
            return ((IEnumerable<KeyValuePair<string, IndexEntry>>) this).GetEnumerator();
        }
    }

    public class IndexEntry : IEnumerable<KeyValuePair<int, Meaning>> {
        private readonly SortedList<int, Meaning> meanings = new SortedList<int, Meaning>();
        
        public void addCase(long position, int meaning, bool recommended) {
            if (!meanings.ContainsKey(meaning)) {
                meanings.Add(meaning, new Meaning());
            }
            meanings[meaning].addCase(new IndexCase(position, recommended));
        }

        public int MeaningLength {
            get { return meanings.Count; }
        }

        public Meaning this[int i] {
            get {
                return meanings[i];
            }
        }

        IEnumerator<KeyValuePair<int, Meaning>> IEnumerable<KeyValuePair<int, Meaning>>.GetEnumerator() {
            return meanings.GetEnumerator();
        }

        public IEnumerator GetEnumerator() {
            return ((IEnumerable<KeyValuePair<int, Meaning>>) this).GetEnumerator();
        }
    }

public class Meaning : IEnumerable<IndexCase> {
        private readonly List<IndexCase> cases = new List<IndexCase>();

        public void addCase(IndexCase indexCase) {
            cases.Add(indexCase);
        }

        public void sort() {
            cases.Sort();
        }

        public int CountCases {
            get {
                return cases.Count;
            }
        }

        public IndexCase this[int i] {
            get {
                return cases[i];
            }
        }

        IEnumerator<IndexCase> IEnumerable<IndexCase>.GetEnumerator() {
            return cases.GetEnumerator();
        }

        public IEnumerator GetEnumerator() {
            return ((IEnumerable<IndexCase>) this).GetEnumerator();
        }

    }

    public class IndexCase : IComparable<IndexCase> {
        private readonly long position;
        private readonly bool recommended;

        public IndexCase(long position, bool recommended) {
            this.position = position;
            this.recommended = recommended;
        }

        public int CompareTo(IndexCase other) {
            if (recommended && ! other.recommended) {
                return 1;
            }
            if (!recommended && other.recommended) {
                return -1;
            }
            return 0;
        }

        public override string ToString() {
            return position.ToString();
        }


        public long Position {
            get { return position; }
        }
    }
}
